<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="关联工单ID" prop="workOrderId">
        <el-input
          v-model="queryParams.workOrderId"
          placeholder="请输入关联工单ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联物料批次ID" prop="materialBatchId">
        <el-input
          v-model="queryParams.materialBatchId"
          placeholder="请输入关联物料批次ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="关联检验标准ID" prop="standardId">
        <el-input
          v-model="queryParams.standardId"
          placeholder="请输入关联检验标准ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="抽样数量" prop="sampleQuantity">
        <el-input
          v-model="queryParams.sampleQuantity"
          placeholder="请输入抽样数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="已检数量" prop="inspectionQuantity">
        <el-input
          v-model="queryParams.inspectionQuantity"
          placeholder="请输入已检数量"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总体判定结果" prop="overallResult">
        <el-input
          v-model="queryParams.overallResult"
          placeholder="请输入总体判定结果"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="指派执行人员ID" prop="assignedToUserId">
        <el-input
          v-model="queryParams.assignedToUserId"
          placeholder="请输入指派执行人员ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="检验开始时间" prop="startTime">
        <el-date-picker clearable
          v-model="queryParams.startTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择检验开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="检验结束时间" prop="endTime">
        <el-date-picker clearable
          v-model="queryParams.endTime"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择检验结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="任务创建时间" prop="createdAt">
        <el-date-picker clearable
          v-model="queryParams.createdAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择任务创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="最后更新时间" prop="updatedAt">
        <el-date-picker clearable
          v-model="queryParams.updatedAt"
          type="date"
          value-format="YYYY-MM-DD"
          placeholder="请选择最后更新时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['quality-service:qualitytasks:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['quality-service:qualitytasks:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['quality-service:qualitytasks:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['quality-service:qualitytasks:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="qualitytasksList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="检验任务ID" align="center" prop="taskId" />
      <el-table-column label="检验类型" align="center" prop="inspectionType" />
      <el-table-column label="关联工单ID" align="center" prop="workOrderId" />
      <el-table-column label="关联物料批次ID" align="center" prop="materialBatchId" />
      <el-table-column label="关联检验标准ID" align="center" prop="standardId" />
      <el-table-column label="抽样数量" align="center" prop="sampleQuantity" />
      <el-table-column label="已检数量" align="center" prop="inspectionQuantity" />
      <el-table-column label="任务状态" align="center" prop="taskStatus" />
      <el-table-column label="总体判定结果" align="center" prop="overallResult" />
      <el-table-column label="指派执行人员ID" align="center" prop="assignedToUserId" />
      <el-table-column label="检验开始时间" align="center" prop="startTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检验结束时间" align="center" prop="endTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="检验过程备注" align="center" prop="notes" />
      <el-table-column label="任务创建时间" align="center" prop="createdAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createdAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="最后更新时间" align="center" prop="updatedAt" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updatedAt, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['quality-service:qualitytasks:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['quality-service:qualitytasks:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改质量检验任务（记录IQC/IPQC/FQC全流程）对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="qualitytasksRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="检验类型" prop="inspectionType">
          <el-input v-model="form.inspectionType" placeholder="检验类型（IQC:来料检/IPQC:制程检/FQC:终检）" />
        </el-form-item>
        <el-form-item label="关联工单ID" prop="workOrderId">
          <el-input v-model="form.workOrderId" placeholder="请输入关联工单ID" />
        </el-form-item>
        <el-form-item label="关联物料批次ID" prop="materialBatchId">
          <el-input v-model="form.materialBatchId" placeholder="请输入关联物料批次ID" />
        </el-form-item>
        <el-form-item label="关联检验标准ID" prop="standardId">
          <el-input v-model="form.standardId" placeholder="请输入关联检验标准ID" />
        </el-form-item>
        <el-form-item label="抽样数量" prop="sampleQuantity">
          <el-input v-model="form.sampleQuantity" placeholder="请输入抽样数量" />
        </el-form-item>
        <el-form-item label="已检数量" prop="inspectionQuantity">
          <el-input v-model="form.inspectionQuantity" placeholder="请输入已检数量" />
        </el-form-item>
        <el-form-item label="总体判定结果" prop="overallResult">
          <el-input v-model="form.overallResult" placeholder="请输入总体判定结果" />
        </el-form-item>
        <el-form-item label="指派执行人员ID" prop="assignedToUserId">
          <el-input v-model="form.assignedToUserId" placeholder="请输入指派执行人员ID" />
        </el-form-item>
        <el-form-item label="检验开始时间" prop="startTime">
          <el-date-picker clearable
            v-model="form.startTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择检验开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检验结束时间" prop="endTime">
          <el-date-picker clearable
            v-model="form.endTime"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择检验结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="检验过程备注" prop="notes">
          <el-input v-model="form.notes" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="任务创建时间" prop="createdAt">
          <el-date-picker clearable
            v-model="form.createdAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择任务创建时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="最后更新时间" prop="updatedAt">
          <el-date-picker clearable
            v-model="form.updatedAt"
            type="date"
            value-format="YYYY-MM-DD"
            placeholder="请选择最后更新时间">
          </el-date-picker>
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Qualitytasks">
import { listQualitytasks, getQualitytasks, delQualitytasks, addQualitytasks, updateQualitytasks } from "@/api/quality-service/qualitytasks"

const { proxy } = getCurrentInstance()

const qualitytasksList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    inspectionType: null,
    workOrderId: null,
    materialBatchId: null,
    standardId: null,
    sampleQuantity: null,
    inspectionQuantity: null,
    taskStatus: null,
    overallResult: null,
    assignedToUserId: null,
    startTime: null,
    endTime: null,
    notes: null,
    createdAt: null,
    updatedAt: null
  },
  rules: {
    inspectionType: [
      { required: true, message: "检验类型不能为空", trigger: "change" }
    ],
    standardId: [
      { required: true, message: "关联检验标准ID不能为空", trigger: "blur" }
    ],
    sampleQuantity: [
      { required: true, message: "抽样数量不能为空", trigger: "blur" }
    ],
    taskStatus: [
      { required: true, message: "任务状态不能为空", trigger: "change" }
    ],
    startTime: [
      { required: true, message: "检验开始时间不能为空", trigger: "blur" }
    ],
    endTime: [
      { required: true, message: "检验结束时间不能为空", trigger: "blur" }
    ],
    createdAt: [
      { required: true, message: "任务创建时间不能为空", trigger: "blur" }
    ],
    updatedAt: [
      { required: true, message: "最后更新时间不能为空", trigger: "blur" }
    ]
  }
})

const { queryParams, form, rules } = toRefs(data)

/** 查询质量检验任务（记录IQC/IPQC/FQC全流程）列表 */
function getList() {
  loading.value = true
  listQualitytasks(queryParams.value).then(response => {
    qualitytasksList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    taskId: null,
    inspectionType: null,
    workOrderId: null,
    materialBatchId: null,
    standardId: null,
    sampleQuantity: null,
    inspectionQuantity: null,
    taskStatus: null,
    overallResult: null,
    assignedToUserId: null,
    startTime: null,
    endTime: null,
    notes: null,
    createdAt: null,
    updatedAt: null
  }
  proxy.resetForm("qualitytasksRef")
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1
  getList()
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef")
  handleQuery()
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.taskId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加质量检验任务（记录IQC/IPQC/FQC全流程）"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _taskId = row.taskId || ids.value
  getQualitytasks(_taskId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改质量检验任务（记录IQC/IPQC/FQC全流程）"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["qualitytasksRef"].validate(valid => {
    if (valid) {
      if (form.value.taskId != null) {
        updateQualitytasks(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addQualitytasks(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _taskIds = row.taskId || ids.value
  proxy.$modal.confirm('是否确认删除质量检验任务（记录IQC/IPQC/FQC全流程）编号为"' + _taskIds + '"的数据项？').then(function() {
    return delQualitytasks(_taskIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('quality-service/qualitytasks/export', {
    ...queryParams.value
  }, `qualitytasks_${new Date().getTime()}.xlsx`)
}

getList()
</script>
